import http from "node:http";
import RequestMethod, {RequestMethodType} from "../../request/RequestMethod";
import URLUtil, {URLParamsObjType} from "../../utils/URLUtil";
import url from "node:url";
import actionStaticRegistrar from "../static/ActionStaticRegistrar";
import ActionStaticResolver from "../static/ActionStaticResolver";

class ActionParam {
    // 请求
    request: http.IncomingMessage
    // 响应
    response: http.ServerResponse
    // 请求路径
    path: string
    // 请求对象
    URL: URL
    // 请求方式
    method: RequestMethodType
    // 请求参数
    params: URLParamsObjType
    // 请求体
    body?: string
    // 是否转发请求
    isForward: boolean = false
    // 转发路线图
    forwardMap: Record<string, {
        parentPath: string,
    }> = {}

    constructor(request: http.IncomingMessage, response: http.ServerResponse) {
        this.request = request
        this.response = response

        const urlVal = url.resolve('http://127.0.0.1/', request.url || '/');
        this.URL = new URL(urlVal);
        this.path = this.URL.pathname

        this.params = URLUtil.URLSearchParamsToObj(this.URL.searchParams)
        this.method = (request.method || RequestMethod.GET) as RequestMethodType;
    }

    forward(path: string) {

    }

    getStatic(staticPath: string) {
        if (!staticPath) return ''
        const staticFilePath = actionStaticRegistrar.get(staticPath);
        if (staticFilePath) {
            return ''
        }
        return ActionStaticResolver.resolve(staticFilePath, this.response)
    }
}

export default ActionParam
